from manim import *



class math2(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{当}$x >0$\text{时，}$\left( 1+x\right) \ln ^{2}\left( 1+x\right)  <x^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
       
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
        self.play(Restore(self.camera.frame))
        #分析文本
        text0=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-35,17.5,0])
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(Create(text0),FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to(UP*20)) 
        #镜头拉近
        self.play(self.camera.frame.animate.scale(0.8).move_to([0,20,0]))

        text1 = Tex(r"\text{令}$F\left( x\right) =\left( 1+x\right) \ln ^{2}\left( 1+x\right) -x^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        self.play(Write(text1),self.camera.frame.animate.move_to([0,15,0]))
        text2 = Tex(r"\text{即证}$F\left( x\right)  <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,10,0])
        self.play(Write(text2),self.camera.frame.animate.move_to([-5,10,0]))
        text3 = Tex(r"$(F\left( 0\right) =0)$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=1)
        self.play(Write(text3))
        #镜头拉回
        self.play(Restore(self.camera.frame))
        #箭头
        #箭头引出
        arrow = Arrow(start=[12,10,0],end=[20,10,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        text4 = Tex(r"$F\left( x\right) \downarrow $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,10,0])
        self.play(Write(text4))

        text5 = Tex(r"$F'\left( x\right) =\ln ^{2}\left( 1+x\right) +2\ln \left( 1+x\right) -2x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text5),self.camera.frame.animate.move_to([-5,10,0]))
        self.play(Restore(self.camera.frame))
        #框框快速闪动
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=45,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([0, 5, 0])
        self.play(Create(rectangle))
        #文本闪动
        text6 = Tex(r"$ <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text5,DOWN,buff=2)
        self.play(Write(text6))
        self.play(FadeOut(text6),FadeOut(rectangle))
        text7 = Tex(r"$(F'\left( 0\right) =0)$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text5,RIGHT,buff=1)
        self.play(Write(text7))

        #箭头引出
        arrow1 = Arrow(start=[10,0,0],end=[20,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow1))
        text8 = Tex(r"$F'\left( x\right) \downarrow $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,0,0])
        self.play(Write(text8))

        #第二次导
        text9 = Tex(r"$F''\left( x\right) =\dfrac{2\ln \left( 1+x\right) }{1+x}+\dfrac{2}{1+x}-2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(Write(text9),self.camera.frame.animate.move_to([0,-5,0]))
        text10 = Tex(r"$F''\left( x\right) =2\dfrac{\ln \left( 1+x\right) -x}{1+x}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(
            ReplacementTransform(text9,text10)
        )
        #框框标记
        #半透明矩阵扩展
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([6,-3,0])
        self.play(
            rectangle1.animate.stretch(16.0,dim=0)
        )
        #文本闪动
        text11 = Tex(r"$ \ln \left( 1+x\right)  <x( x > 0) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text10,DOWN,buff=2)
        self.play(Write(text11))
        self.play(FadeOut(text11))
        #文本
        text12 = Tex(r"$  <0 $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text10,RIGHT,buff=2)
        self.play(Write(text12))
        #框框移动
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([20, -5, 0])
        self.play(Create(rectangle2))
        self.play(rectangle2.animate().move_to([25,0,0]))
        self.play(rectangle2.animate().move_to([-17,5,0]))
        #文本闪动
        text13 = Tex(r"$  <0 $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle2,DOWN,buff=2)
        self.play(Write(text13))
        self.play(FadeOut(text13))
        self.play(rectangle2.animate().move_to([25,10,0]))
        self.play(rectangle2.animate().move_to([-7,10,0]).stretch(1.2,dim=0))

        #清空屏幕
        self.clear()
        #文本

        text13 = Tex(r"\text{(2012)证明：}$x\ln \dfrac{1+x}{1-x}+\cos x\geq 1+\dfrac{1}{2}x^{2},  $\text{其中}$-1\leq x\leq 1$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text13),self.camera.frame.animate.scale(0.7).move_to([0,0,0]))









        self.play(Restore(self.camera.frame))
        self.wait(1)
